package com.dhl.fin.api.service;

import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.exception.BusinessException;
import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.StringUtil;
import com.dhl.fin.api.common.util.WebUtil;
import com.dhl.fin.api.dao.MyFamilyConfigDao;
import com.dhl.fin.api.domain.FamilyConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FamilyConfigService extends CommonService<FamilyConfig> {

    @Autowired
    private MyFamilyConfigDao myFamilyConfigDao;

    public void initFamilyConfig(String familyUnicode) {
        String user = WebUtil.getLoginUser().getUuid();
        myFamilyConfigDao.resetFamilyConfig(familyUnicode);
        myFamilyConfigDao.initConfig(user, familyUnicode);
        myFamilyConfigDao.initTag(user, familyUnicode);
    }

    public List<FamilyConfig> getYearRateConfigs(String familyUnicode) throws Exception {
        return select(QueryDto.builder()
                .available()
                .addWhere("family_unicode='" + familyUnicode + "'")
                .addWhere("name like 'yearRate%'")
                .addOrder("sort")
                .build());
    }

    public String getPenalty(String familyUnicode) throws Exception {
        return get(QueryDto.builder()
                .available()
                .addWhere("family_unicode='" + familyUnicode + "'")
                .addWhere("name = 'fixTimePenalty'")
                .build()).getValue();
    }

    public Map<String, String> getAllFamilyTimeRateConfigs() throws Exception {
        return select(QueryDto.builder()
                .available()
                .addWhere("name = 'timeYearRate'")
                .build())
                .stream()
                .collect(Collectors.toMap(FamilyConfig::getFamilyUnicode, FamilyConfig::getValue, (x, y) -> x));
    }

    public String getTimeYearRate() throws Exception {
        String unicode = WebUtil.getStringParam("faUnicode");
        FamilyConfig familyConfig = get(QueryDto.builder()
                .available()
                .addWhere("name = 'timeYearRate'")
                .addWhere("family_unicode = '" + unicode + "'")
                .build());

        return ObjectUtil.isNull(familyConfig) ? "" : familyConfig.getValue();
    }

    public String getLendYearRate() throws Exception {
        String unicode = WebUtil.getStringParam("faUnicode");
        FamilyConfig familyConfig = get(QueryDto.builder()
                .available()
                .addWhere("name = 'lendRate'")
                .addWhere("family_unicode = '" + unicode + "'")
                .build());
        return ObjectUtil.isNull(familyConfig) ? "" : familyConfig.getValue();
    }

    public String getLendPenaltyYearRate() throws Exception {
        String unicode = WebUtil.getStringParam("faUnicode");
        FamilyConfig familyConfig = get(QueryDto.builder()
                .available()
                .addWhere("name = 'lendPenalty'")
                .addWhere("family_unicode = '" + unicode + "'")
                .build());
        return ObjectUtil.isNull(familyConfig) ? "" : familyConfig.getValue();
    }

    public FamilyConfig checkPasswordRight(String familyUnicode, String password) throws Exception {
        FamilyConfig familyConfig = get(QueryDto.builder()
                .available()
                .addWhere("name = 'password'")
                .addWhere("family_unicode='" + familyUnicode + "'")
                .addWhere("value = '" + password + "'")
                .build());
        if (ObjectUtil.isNull(familyConfig)) {
            throw new BusinessException("密码不正确");
        }
        return familyConfig;
    }

    public void updateFamilyPassword(String familyUnicode, String oldPassword, String newPassword) throws Exception {

        if (StringUtil.isNotEmpty(oldPassword)) {
            FamilyConfig familyConfig = null;

            try {
                //密码校验
                familyConfig = checkPasswordRight(familyUnicode, oldPassword);
            } catch (BusinessException e) {
                throw new BusinessException("原始密码不正确");
            }

            //更新密码
            updateFieldById(familyConfig.getId(), "value", newPassword);

        } else {
            FamilyConfig familyConfig = new FamilyConfig();
            familyConfig.setFamilyUnicode(familyUnicode);
            familyConfig.setName("password");
            familyConfig.setValue(newPassword);
            saveDomain(familyConfig);
        }


    }

}
